package net.wzw.config;

import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.api.RulesEngineParameters;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;

import java.io.IOException;

/**
 * @author wzw
 * @date 2025/4/2
 * @Description
 */
@Configuration
@Slf4j
public class RuleEngineConfig {

    @Bean
    public RulesEngine rulesEngine() {

        return new DefaultRulesEngine(
                new RulesEngineParameters()
                        //跳过未触发规则执行下一条
                        .skipOnFirstNonTriggeredRule(true)
                        //优先级
                        .priorityThreshold(10)
        );

    }

//    @Bean
//    public Rules ruleRegister() throws IOException {
//
//
//        return new Rules(
//                new AnnotationRuleFactory().create(
//                        new ClasspathRuleDefinitionReader(),
//                        new ClassPathResource("rules/").getFile()
//                )
//        );
//    }

    @Bean
    public ApplicationRunner ruleInitializer(RulesEngine engine, Rules rules) {

        return args -> {
            // 启动时预加载验证规则
            engine.fire(rules, new Facts());
            log.info("已成功加载{}条规则", rules.size());
        };


    }

}
